/*

BlackRabbit - OpenSource IDE.
@author Edward Sarkisyan (edward.sarkisyan@gmail.com)
Use LGPL (http://www.gnu.org/copyleft/lesser.html)

*/

#include "newfiledialog.h"
#include "ui_newfiledialog.h"
#include <QFile>
#include <QMessageBox>
#include <QFileDialog>

NewFileDialog::NewFileDialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::NewFileDialog)
{
    m_ui->setupUi(this);
}

NewFileDialog::~NewFileDialog()
{
    delete m_ui;
}

void NewFileDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void NewFileDialog::on_buttonBox_accepted()
{
    if(m_ui->fileNameEdit->text().isEmpty())
    {
        QMessageBox::warning(this, tr("No file name specified"), tr("You must enter valid file name for new file"));
        return;
    }
    if(m_ui->filePathEdit->text().isEmpty())
    {
        QMessageBox::warning(this, tr("No file path specified"), tr("You must enter valid file path for new file"));
        return;
    }
    m_fileName = m_ui->filePathEdit->text().append('/').append(m_ui->fileNameEdit->text());
    if(QFile::exists(m_fileName))
    {
        QMessageBox *mess = new QMessageBox(this);
        if(mess->question(this,tr("File already exists."),tr("File already exists, do you want to use existing file?"),
                       QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::No)
        {
            m_fileName.clear();
            this->close();
        }
        delete mess;
    }
}

QString NewFileDialog::getFileName()
{
    return m_fileName;
}

void NewFileDialog::on_addPathBtn_clicked()
{
    m_ui->filePathEdit->setText(QFileDialog::getExistingDirectory(this, tr("Choose directory for new file")));
}
